import time
from common_1.Ultils import ForumDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains

class ForumBlogDetail:
    url=""
    driver=""
    def __init__(self):
        url="http://127.0.0.1:9580/index.html"
        self.driver=ForumDriver.driver
        self.driver.get(url)

    def ForumBlogDetailTest(self):
        # 点击第二篇博客（修正选择器）
        blog_entry = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((
                By.CSS_SELECTOR,
                "#artical-items-body > div:nth-child(4) > div > div.col > div.text-truncate > a > strong"
            ))
        )
        blog_entry.click()

        # 等待详情页加载完成
        WebDriverWait(self.driver, 15).until(
            # EC.url_contains("#bit-forum-content"),  # 根据实际URL调整
            # "未进入详情页"
            EC.element_to_be_clickable(
                (By.CSS_SELECTOR,"#bit-forum-content")
            )
        )
        print("页面加载成功")

        # 显式等待作者头像
        WebDriverWait(self.driver, 15).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-body")),
            "作者帖子内容加载超时"
        )

        # 博客用户昵称
        self.driver.find_element(By.CSS_SELECTOR, "#article_details_author_name")
        # 用户头像
        self.driver.find_element(By.CSS_SELECTOR, "#article_details_author_avatar")
        # # 博客所收的反馈信息
        # self.driver.find_element(By.CSS_SELECTOR, "#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(4)")

        # 帖子标题
        self.driver.find_element(By.CSS_SELECTOR, "#details_article_content_title")

        # 帖子内容
        self.driver.find_element(By.CSS_SELECTOR, "#details_article_content > p")

        # 评论区列表
        self.driver.find_element(By.CSS_SELECTOR, "#details_reply_area")

        # 检测私信弹窗的出现（此弹窗可以定位）——发送成功，跳出“发送成功”弹窗
        self.driver.find_element(By.CSS_SELECTOR, "#btn_details_send_message").click()
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_receive_content").send_keys("cool!")
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_send_message").click()
        # 等待发送成功的时间
        time.sleep(5)
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#index_message_modal > div > div > div.modal-header > button").click()
        # 等待弹窗的消失
        WebDriverWait(self.driver, 15).until(
            EC.invisibility_of_element_located((By.CSS_SELECTOR,"#index_message_modal > div > div"))
        )


        # 点赞按钮
        like_button = WebDriverWait(self.driver, 15).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "#details_btn_like_count"))
        )
        like_button.click()
        # 等待点赞成功时间
        time.sleep(5)

        reply_button = WebDriverWait(self.driver, 15).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "#details_btn_article_reply"))
        )

        # 滚动元素到视窗中心
        actions = ActionChains(self.driver)
        actions.move_to_element(reply_button).perform()

        # 验证评论是否能够发送成功
        self.driver.find_element(By.CSS_SELECTOR, "#article_details_reply > div.editormd-toolbar > div > ul > li:nth-child(12) > a > i").click()

        # 回复按钮
        reply_button.click()

        print("详情页元素验证通过")

